<?php
// +----------------------------------------------------------------------
// | B5Yii2CMF V3.0 [快捷通用基础管理开发平台]
// +----------------------------------------------------------------------
// | Author: 冰舞 <357145480@qq.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace common\models\system;

use yii\behaviors\TimestampBehavior;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "sys_role".
 *
 * @property int $id 角色ID
 * @property string|null $name 角色名称
 * @property string|null $role_key 角色权限字符串
 * @property int|null $check_strictly 是否父子关联
 * @property int|null $list_sort 显示顺序
 * @property string|null $data_scope 数据范围
 * @property string|null $status 角色状态（1正常 0停用）
 * @property string|null $remark 备注
 * @property int|null $create_time 创建时间
 * @property int|null $update_time 更新时间
 * @property-read RoleMenu[]|null $menuIds
 * @property-read RoleDept[]|null $deptIds
 */
class Role extends ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName(): string
    {
        return 'sys_role';
    }

    /**
     * {@inheritdoc}
     */
    public function rules(): array
    {
        return [
            [['name'], 'required'],

            [['role_key', 'remark'], 'default', 'value' => ''],
            ['status', 'default', 'value' => '1'],
            ['list_sort', 'default', 'value' => 10],
            [['data_scope', 'check_strictly'], 'default', 'value' => 0],

            [['name', 'role_key', 'status', 'remark'], 'trim'],

            ['role_key', 'match', 'pattern' => '/^[A-Za-z0-9_-]+$/', 'message' => '{attribute}必须是字母、数字、下划线或破折号'],

            ['name', 'string', 'max' => 64],
            ['role_key', 'string', 'max' => 128],
            ['remark', 'string', 'max' => 250],
            [['list_sort', 'data_scope'], 'integer'],
            ['status', 'in', 'range' => ['0', '1']],

            [['create_time', 'update_time'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels(): array
    {
        return [
            'id' => '角色ID',
            'name' => '角色名称',
            'role_key' => '权限字符',
            'check_strictly' => '是否父子关联',
            'data_scope' => '数据范围',
            'status' => '角色状态',
            'list_sort' => '显示顺序',
            'remark' => '备注',
            'create_time' => '创建时间',
            'update_time' => '更新时间',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function afterDelete()
    {
        if ($this->id) {
            AdminRole::deleteAll(['role_id' => $this->id]);
            RoleMenu::deleteAll(['role_id' => $this->id]);
            RoleDept::deleteAll(['role_id' => $this->id]);
        }
        parent::afterDelete();
    }

    /**
     * {@inheritdoc}
     */
    public function behaviors(): array
    {
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['create_time', 'update_time'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['update_time'],
                ],
                'value' => function () {
                    return time();
                }
            ]
        ];
    }

    public function getMenuIds(): ActiveQuery
    {
        return $this->hasMany(RoleMenu::class, ['role_id' => 'id']);
    }

    public function getDeptIds(): ActiveQuery
    {
        return $this->hasMany(RoleDept::class, ['role_id' => 'id']);
    }
}
